﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using G.Navigate;
using G.WeiBo.WebAPI.Domain;
using G.WeiBo.WebAPI.Request;
using G.WeiBo.WebAPI.Response;
using G.WeiBo.WebAPI;
using System.Collections.ObjectModel;
using System.Windows.Controls.Primitives;

namespace G.WeiBo.View
{
    public partial class MyPage : GBasePage
    {
        DefaultClient client = GlobalStaticData.Client;
        public MyPage()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (gridUserInfo.DataContext == null)
            {
                //this.gridUserInfo.DataContext = GlobalStaticData.UserInfo;

                client.Execute(new GetUserInfoRequest { UserId = G.Net.Oauth.OAuth2AccessToken.CurrentToken.Uid }, (c) =>
                {
                    if (!c.IsError)
                    {
                        this.gridUserInfo.DataContext = c.Response;
                        //SetUserInfo();
                    }
                });
            }
        }
        private void TextButton_Click(object sender, RoutedEventArgs e)
        {
            ButtonBase btn = sender as ButtonBase;
            this.pivot.SelectedIndex = int.Parse(btn.Tag as string);
        }

        private void listBoxtFaviorat_BeginLoadData(object sender, Controls.PageEventArgs e)
        {
            client.Execute(new GetFavoritesRequest { Page = e.PageNum }, (c) =>
            {
                this.listBoxtFaviorat.LoadData<Message>(c.Favorites.Select(a => a.Message));
            });
        }

        private void listBoxtAtMy_BeginLoadData(object sender, Controls.PageEventArgs e)
        {

            ObservableCollection<Message> itemsSource = this.listBoxtAtMy.ItemsSource as ObservableCollection<Message>;
            if (itemsSource != null && itemsSource.Count > 0)
            {
                client.Execute(new GetMentionsUserMessagesRequeste { Count = e.PageSize, PageIndex = e.PageNum, MaxId = itemsSource.Last().Id }, (c) =>
                {
                    this.listBoxtAtMy.LoadData<Message>(c.Messages);
                });
            }
            else
            {
                client.Execute(new GetMentionsUserMessagesRequeste { Count = e.PageSize, PageIndex = e.PageNum }, (c) =>
                {
                    this.listBoxtAtMy.LoadData<Message>(c.Messages);
                });
            }
        }

        private void listBoxtFans_BeginLoadData(object sender, Controls.PageEventArgs e)
        {
            int pageNum = -1;
            object nextPage = this.ReadFromState("listBoxtFansNextPage");
            if (nextPage != null)
                pageNum = (int)nextPage;

            GlobalStaticData.Client.Execute(new GetFansRequest { UserId = G.Net.Oauth.OAuth2AccessToken.CurrentToken.Uid, Page = pageNum, Count = e.PageSize }, (c) =>
            {
                this.SaveToState("listBoxtFansNextPage", c.NextPage);
                listBoxtFans.LoadData<UserInfo>(c.Users);
                if (c.Users == null || c.Users.Count() < 1)
                {
                    listBoxtFans.CancelPageListBox();
                }
            });
        }

        private void listBoxtFriends_BeginLoadData(object sender, Controls.PageEventArgs e)
        {
            int pageNum = 0;
            object nextPage = this.ReadFromState("listBoxtFriendsNextPage");
            if (nextPage != null)
                pageNum = (int)nextPage;
            //if (pageNum == 0)
            //{
            //    listBoxtFriends.CancelPageListBox();
            //    return;
            //}
            GlobalStaticData.Client.Execute(new GetFriendsRequest { UserId = G.Net.Oauth.OAuth2AccessToken.CurrentToken.Uid, Page = pageNum, Count = e.PageSize }, (c) =>
            {
                this.SaveToState("listBoxtFriendsNextPage", c.NextPage);
                listBoxtFriends.LoadData<UserInfo>(c.Users);
                if (c.Users == null || c.Users.Count() < 1)
                {
                    listBoxtFriends.CancelPageListBox();
                }
            });
        }

        private void listBoxtUserMessage_BeginLoadData(object sender, Controls.PageEventArgs e)
        {
            GlobalStaticData.Client.Execute(new GetUserTimelineRequest { UserID = G.Net.Oauth.OAuth2AccessToken.CurrentToken.Uid, Page = e.PageNum, Count = e.PageSize }, (c) =>
            {
                if (c.Messages == null || c.Messages.Count() < 1)
                {
                    listBoxtUserMessage.CancelPageListBox();
                }
                listBoxtUserMessage.LoadData<Message>(c.Messages);
            });
        }

        private void listBoxtUserMessage_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            GetDatas();
        }
        private void GetDatas()
        {
            if (pivot.SelectedIndex == 1 && listBoxtUserMessage.ItemsSource == null)
            {
                listBoxtUserMessage.EnableTombstoneListBox<Message>(this);
            }
            else if (pivot.SelectedIndex == 2 && listBoxtFriends.ItemsSource == null)
            {
                this.SaveToState("listBoxtFriendsNextPage", -1);
                this.listBoxtFriends.EnableTombstoneListBox<UserInfo>(this);
            }
            else if (pivot.SelectedIndex == 3 && listBoxtFans.ItemsSource == null)
            {
                this.SaveToState("listBoxtFansNextPage", -1);
                this.listBoxtFans.EnableTombstoneListBox<UserInfo>(this);
            }
            else if (pivot.SelectedIndex == 4 && listBoxtAtMy.ItemsSource == null)
            {
                this.listBoxtAtMy.EnableTombstoneListBox<UserInfo>(this);
            }
            else if (pivot.SelectedIndex == 5 && listBoxtFaviorat.ItemsSource == null)
            {
                this.listBoxtFaviorat.EnableTombstoneListBox<UserInfo>(this);
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Refresh();
        }
        private void Refresh()
        {
            if (pivot.SelectedIndex == 1)
            {
                listBoxtUserMessage.ClearItems();
            }
            else if (pivot.SelectedIndex == 2)
            {
                this.listBoxtFriends.ClearItems();
            }
            else if (pivot.SelectedIndex == 3)
            {
                this.listBoxtFans.ClearItems();
            }
            else if (pivot.SelectedIndex == 4)
            {
                this.listBoxtAtMy.ClearItems();
            }
            else if (pivot.SelectedIndex == 5)
            {
                this.listBoxtFaviorat.ClearItems();
            }
            GetDatas();
        }



        private void menuDeleteMessage_Click(object sender, RoutedEventArgs e)
        {
            Message messag = (sender as FrameworkElement).DataContext as Message;
            GlobalStaticData.Client.Execute(new DeleteMessageRequest { ID = messag.Id, }, (c) =>
            {
                if (!c.IsError)
                {
                    this.ShowState("删除成功！");
                    this.listBoxtUserMessage.RemoveItem<Message>(messag);
                }
            });
        }

        private void menuDeleteFavior_Click(object sender, RoutedEventArgs e)
        {
            Message messag = (sender as FrameworkElement).DataContext as Message;
            GlobalStaticData.Client.Execute(new DeleteFavoriteRequest { ID = messag.Id, }, (c) =>
            {
                if (!c.IsError)
                {
                    this.ShowState("删除成功！");
                    this.listBoxtFaviorat.RemoveItem<Message>(messag);
                }
            });
        }
    }
}